/* -----------------------------------------------------------------------------
 * SNMP implementation for Contiki
 *
 * Copyright (C) 2010 Siarhei Kuryla <kurilo@gmail.com>
 *
 * This program is part of free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <stdio.h>
#include <string.h>

#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"

#include "snmpd.h"
#include "snmpd-conf.h"
#include "logging.h"

#define UDP_IP_BUF   ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])

/* UDP connection */
static struct uip_udp_conn *udpconn;

PROCESS(snmpd_process, "SNMP daemon process");

#include "aes.h"

int encrypt(u8t * key, u8t * iv,
            u8t * plaintext, u16t ptlen,
            u8t * ciphertext)
{
    AES_KEY aes_key;
    int new_ivlen = 0;

    AES_set_encrypt_key(key, 128, &aes_key);
    AES_cfb128_encrypt(plaintext, ciphertext, ptlen, &aes_key, iv, &new_ivlen, AES_ENCRYPT);
    return 0;
}

int decrypt(u8t * key, u8t * iv,
            u8t * plaintext, u16t ptlen,
            u8t * ciphertext)
{
    AES_KEY aes_key;
    int new_ivlen = 0;

    AES_set_encrypt_key(key, 128, &aes_key);
    AES_cfb128_encrypt(plaintext, ciphertext, ptlen, &aes_key, iv, &new_ivlen, AES_DECRYPT);
    return 0;
}

static void print_as_hex (const unsigned char *digest, int len) {
    int i;
    for(i = 0; i < len; i++) {
        printf ("%02x", digest[i]);
    }
    printf("\n");
}


unsigned char key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};


unsigned char input[48] =     {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
                               0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
                               0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef};

unsigned char output[48], decodedtext[48];
/*-----------------------------------------------------------------------------------*/
/*
 *  Entry point of the SNMP server.
 */
PROCESS_THREAD(snmpd_process, ev, data) {
	PROCESS_BEGIN();


    // 3b3fd92eb72dad20333449f8e83cfb4ac8a64537a0b3a93fcde3cdad9f1ce58b26751f67a3cbb140b1808cf187a4f4df

	udpconn = udp_new(NULL, HTONS(0), NULL);
	udp_bind(udpconn, HTONS(LISTEN_PORT));

        while(1) {
            PROCESS_YIELD();
            //udp_handler(ev, data);
            snmp_info("PLAN: %02x%02x%02x%02x\n", input[0], input[1], input[2], input[3]);
            unsigned char iv[48] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
            encrypt(key, iv, input, 48, output);
            snmp_info("ECNR: %02x%02x%02x%02x | %02x%02x%02x%02x\n", output[0], output[1], output[2], output[3], output[44], output[45], output[46], output[47]);
            //print_as_hex(output, 48);
            /*unsigned char iv2[48] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
            decrypt(key, iv2, output, 48, decodedtext);
            print_as_hex(input, 48);
            print_as_hex(decodedtext, 48);*/
            //snmp_info("C: %02x%02x%02x%02x", decodedtext[0], decodedtext[1], decodedtext[2], decodedtext[3]);
        }
	PROCESS_END();
}
/*-----------------------------------------------------------------------------------*/
